//
//  YZXLoginView.swift
//  YZXWeibo
//
//  Created by 叶周翔 on 16/7/18.
//  Copyright © 2016年 叶周翔. All rights reserved.
//

import UIKit

protocol loginViewDelegate:NSObjectProtocol {
    // 定义代理方法
    
    func loginBtnDidClick()
    
}

class YZXLoginView: UIView {
    // 定义代理属性
    weak var delegate:loginViewDelegate?
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override init(frame: CGRect) {
       super.init(frame: frame)
//        self.backgroundColor = UIColor.grayColor()

        // 设置背景颜色  
        self.backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        // 添加按钮和图片
        self.setUpSubViews()
        
    }
    
    // MARK:
    /**
       外部调用这个方法时 设置标题和图片
    */
    func setUplogView(imageName:String, title:String){
        
        houseView.hidden = true
        iconView.image = UIImage(named: imageName)
        // 将遮罩的图片添加到后面去
        self.sendSubviewToBack(shadeView)
        messageLabel.text = title
    }
    
    @objc private func logBtnDidClick(){
        // 使用代理实现
        delegate?.loginBtnDidClick()
        
    }
    
    
    // MARK:布局子控件
    func setUpSubViews() -> Void{
        
        // 将所有的控件添加到view中
        self.addSubview(iconView)
        self.addSubview(shadeView)
        self.addSubview(houseView)
        self.addSubview(messageLabel)
        self.addSubview(registerBtn)
        self.addSubview(loginBtn)
        
        // 设置约束  去除Autoresizing
        iconView.translatesAutoresizingMaskIntoConstraints = false
        shadeView.translatesAutoresizingMaskIntoConstraints = false
        houseView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        registerBtn.translatesAutoresizingMaskIntoConstraints = false
        loginBtn.translatesAutoresizingMaskIntoConstraints = false
        
        // 设置约束
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        // 遮罩图片的约束
        self.addConstraint(NSLayoutConstraint(item: shadeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: shadeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // houseView的约束
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: houseView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        // 消息文本标签的约束
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 260))

        
        // 注册按钮的约束
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: registerBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 30))
        
        
        // 登录按钮的约束
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 100))
        self.addConstraint(NSLayoutConstraint(item: loginBtn, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 30))
    }
    // MARK:图片的旋转动画
    func startAnimation() -> Void{
        // 核心动画实现
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 动画的次数
        anim.repeatCount = MAXFLOAT
        // 持续时间
        anim.toValue = 2 * M_PI
        // 持续时间
        anim.duration = 10
        anim.removedOnCompletion = false
        // 添加到图层中
        self.iconView.layer.addAnimation(anim, forKey: nil)
    }
    
    // MARK:懒加载
    /// 懒加载转动的imageView
    lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    /// 遮罩图片
    lazy var shadeView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    
    /// 中心房子
    lazy var houseView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    /// label
    lazy var messageLabel:UILabel = {
        let label = UILabel()
        // 设置label的样式
        label.textAlignment = NSTextAlignment.Center
        label.textColor = UIColor.darkGrayColor()
        label.text = "关于建设社会主义社会的唯一标准就是这个世界上真的很操蛋"
        label.textColor = UIColor.redColor()
        // 换行
        label.numberOfLines = 0
        return label
    }()
    
    /// 注册按钮
    lazy var registerBtn:UIButton = {
       let btn = UIButton(type: UIButtonType.Custom)
        btn.setTitle("注册", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        
        
        return btn
    }()
    
    
    /// 登录按钮
    lazy var loginBtn:UIButton = {
        let btn = UIButton(type: UIButtonType.Custom)
       btn.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        btn.setTitle("登录", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        // 添加点击事件
        btn.addTarget(self, action: "logBtnDidClick", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    
    
}
